<?php
namespace app\api\controller\vuser;
class User extends Base
{
    //用户使用微信openid登录
    public function login(){
        if(is_post()){
      		$d = $this->post();
      		$rule = [
      			'wx_openid' => 'require',
      		];
      		$msg = [];
      		check($rule, $msg, $d);
          $user = $this->db->query("select * from user where wx_openid=?",[$d['wx_openid']],1);
          $user['token'] = token($user['id']);
          $this->db->update('user', ['id'=>$user['id'],'token'=>$user['token']]);
          suc($user);
        }
    }

    //获取用户信息
    public function getinfo(){
      $this->checkLogin();
      $user = $this->userinfo;
      $user['level_name'] = c("type.user_level")[$user['level']];
      suc($user);
    }

    //用户汇总信息
    public function count(){
      $this->checkLogin();
      $rs = $this->db->query("select count(*) as total from user where s_tree like ?",["{$this->userinfo['s_tree']}{$this->userinfo['id']},%"],1);
      $rs1 = $this->db->query("select count(*) as total from user where s_id =?",[$this->userinfo['id']],1);
      $rs2 = $this->db->query("select sum(money) as total from withdraw where user_id =? and status=2",[$this->userinfo['id']],1);
      $rs3 = $this->db->query("select sum(money) as total from user_bill where user_id =? and type in (100,101,102)",[$this->userinfo['id']],1);
      $data = [
        'alluser'=>$rs['total'] ? $rs['total'] : 0,
        'myuser'=>$rs1['total'] ? $rs1['total'] : 0,
        'withdraw'=>$rs2['total'] ? $rs2['total'] : 0,
        'income'=>$rs3['total'] ? $rs3['total'] : 0,
      ];
      suc($data);
    }

    //编辑用户信息
    public function edit(){
      $this->checkLogin();
      $d = $this->post();
      $rule = [
        'key' => 'require',
        'value' => 'require',
      ];
      $msg = [];
      check($rule, $msg, $d);
      if(!in_array($d['key'], ['username','name','phone','pic'])){
        err('非法字段');
      }
      $data = [
        'id' => $this->userinfo['id'],
        "{$d['key']}" => $d['value']
      ];
      $this->db->update('user',$data);
      suc();
    }

    //用户列表
    public function lists(){
      $this->checkLogin();
      $d = $this->post();
      $rule = [
        'type' => 'require',
        'page' => 'require',
        'limit' => 'require',
      ];
      $msg = [];
      check($rule, $msg, $d);
      if($d['type']==0){
        $rs = $this->db->query("select id,pic,username,name,phone,create_time,level from user where s_tree like ? order by id desc ". limit($d['page'], $d['limit']),["{$this->userinfo['s_tree']}{$this->userinfo['id']},%"]);
      }else{
        $rs = $this->db->query("select id,pic,username,name,phone,create_time,level from user where s_id = ? order by id desc ". limit($d['page'], $d['limit']),[$this->userinfo['id']]);
      }
      foreach($rs as $k => $v){
        $rs[$k]['pic'] = pic($v['pic']);
        $rs[$k]['create_time_name'] = com_time_format($v['create_time'],'Y-m-d');
        $rs[$k]['level_name'] = c("type.user_level.{$v['level']}");
      }
      suc($rs);
    }
}
